
import lazyLoading from './lazyLoading'
export default (routers, data) => {
  // 这里之所以要重新遍历一下，是因为，通常我们动态路由的时候，是获取服务端数据，这个component属性是一个字符串，
  // 或者可能连字段名都是其他的key
  // 所以这里要做一些转换
  var data = {
    path: '/multilevel',
    name: 'multilevel',
    meta: {
      icon: 'md-menu',
      title: '多级菜单'
    },
    component: 'Main',
    children: [
      {
        path: 'level_2_1',
        name: 'level_2_1',
        meta: {
          icon: 'md-funnel',
          title: '二级-1'
        },
        component: 'multilevel/level-2-1'
      },
      {
        path: 'level_2_2',
        name: 'level_2_2',
        meta: {
          access: ['super_admin'],
          icon: 'md-funnel',
          showAlways: true,
          title: '二级-2'
        },
        component: 'parentView',
        children: [
          {
            path: 'level_2_2_1',
            name: 'level_2_2_1',
            meta: {
              icon: 'md-funnel',
              title: '三级'
            },
            component: 'multilevel/level-2-2/level-3-1'
          }
        ]
      },
      {
        path: 'level_2_3',
        name: 'level_2_3',
        meta: {
          icon: 'md-funnel',
          title: '二级-3'
        },
        component: 'multilevel/level-2-3'
      }
    ]
  };
  console.log(111)
  console.log(2222)
  generaMenu(routers, data)
}
function generaMenu (routers, data) {
  data.forEach((item) => {
    let menu = Object.assign({}, item)
    menu.component = lazyLoading(menu.component)
    if (item.leaf && item.children && item.children.length === 1) {
      menu.children = []
      generaMenu(menu.children, item.children)
    } else if (!item.leaf) {
      menu.children = []
      generaMenu(menu.children, item.children)
    }
    routers.push(menu)
  })
}
